package com.peng.consistenthash;

import com.google.common.base.Joiner;
import org.junit.Before;
import org.junit.Test;

import java.util.Random;

/**
 * ConsistentHashTest
 *
 * @author: lupeng10
 * @create: 2023-04-18 19:45
 */
public class ConsistentHashTest {

    private SortedMapVirtualNodeBalancer virtualNodeBalancer;

    @Before
    public void init() {
        this.virtualNodeBalancer = new SortedMapVirtualNodeBalancer(10);
        for (int i = 0; i < 100; i++) {
            this.virtualNodeBalancer.addServerNode(generateIpAddress());
        }
    }

    private String generateIpAddress() {
        Random random = new Random();
        String ip = Joiner.on(".").join(random.nextInt(256), random.nextInt(256), random.nextInt(256), random.nextInt(256));
        String port = random.nextInt(9999) + "";
        return ip + ":" + port;
    }

    @Test
    public void t1() {
        virtualNodeBalancer.a1();
        // Map<String, String> map = new HashMap<>();
        // for (int i = 0; i < 1000; i++) {
        //     String requestAddress = generateIpAddress();
        //     String serverNode = virtualNodeBalancer.selectServerNode(requestAddress);
        //     map.put(requestAddress, serverNode);
        // }
        //
        // System.out.println("方差：" + new Analysis().analysis(map));
    }

}
